$(document).ready(function(){
		
		$("#loginBtn").click(function(){
			if (checkForm()){
				doLogin($("#username").val(), $("#password").val());
			} else {
				alert("Invalid credentials. Are you trying to hack?");
			}
		});

		$("#username").focus();

		$("#password").keypress(function(event){
			if(event.keyCode == '13') { 
				$("#loginBtn").click();
			}
		});
		
		
		$("button").button();
});


function showPleaseWait(){
	$("#message").html("Sending email, please wait...");
	$("#message").dialog({
		bgiframe :true,
		autoOpen :false,
		width: 300,
		modal :true,
		resizable: false,
		title: "Please wait...",
		buttons : {
		}
	});
	$("#message").dialog("open");
}

function resetPassword(username){
	var url="/index.php?mod=controller1";
	var parms = new Object();
	parms["action"]= "resetPassword";
	parms["username"]= username;

	$.post(url,parms,function(resultData){
		var errorCode = $(resultData).find("code").text();
		if(errorCode == 0){
			$("#message").dialog("destroy");
			alert("Your password has been reset. Check your email.");
	} else {
			$("#message").dialog("destroy");
			alert($(resultData).find("errMessage").text());
			$("#password").val("");
			$("#password").focus();
		} 
	});
}

function createUser(username, email, name, surname){
	var url="/index.php?mod=controller1";
	var parms = new Object();
	parms["action"]= "createUser";
	parms["username"]= username;
	parms["email"]= email;
	parms["name"]= name;
	parms["surname"]= surname;

	$.post(url,parms,function(resultData){
		var errorCode = $(resultData).find("code").text();
		if(errorCode == 0){
			$("#message").dialog("destroy");
			alert("Your account has been created successfully. Check your email for your password.");
	} else {
			$("#message").dialog("destroy");
			alert($(resultData).find("errMessage").text());
		} 
	});
}

function showCreateUserBox(){
	$("#newusername").val("");
	$("#name").val("");
	$("#surname").val("");
	$("#emailaddress").val("");
	$("#confirmemailaddress").val("");
		
	$("#createUserBox").dialog({
		bgiframe :true,
		autoOpen :false,
		width: 400,
		maxHeight: 200,
		modal :true,
		resizable: false,
		title: "Create Account",
		buttons : {
			Save : function() {
				createUser($("#newusername").val(), $("#emailaddress").val(), $("#name").val(), $("#surname").val());
				$(this).dialog('destroy');
			},
			Cancel : function() {
				$(this).dialog('destroy');
			}
		}
	});
	
	$("#createUserBox").dialog("open");
	$("#newusername").focus();
}

function showChangePasswordBox(){
	$("#forgotusername").val("");
		
	$("#forgotPasswordBox").dialog({
		bgiframe :true,
		autoOpen :false,
		width: 400,
		maxHeight: 200,
		modal :true,
		resizable: false,
		buttons : {
			Submit : function() {
				resetPassword($("#forgotusername").val());
				$(this).dialog('destroy');
			},
			Cancel : function() {
				$(this).dialog('destroy');
			}
		}
	});
	
	$("#forgotPasswordBox").dialog("open");
	$("#forgotusername").focus();
}

function checkForm(){
	if ($("#username").val() != "" && $("#password").val() != ""){
		 return true;
	} else{
		 return false;
	}
}

function doLogin(username, password){
	var url="/index.php?mod=controller1";
	var parms = new Object();
	parms["action"]= "loginUser";
	parms["username"]= username;
	parms["password"]= password;
	
	$.post(url,parms,function(resultData){
	
			//alert(resultData);
		
		//return;
		var errorCode = $(resultData).find("code").text();
		if(errorCode == 0){
			top.location = "index.php?mod=myaccounts";
		} else {
			alert($(resultData).find("errMessage").text());
			$("#password").val("");
			$("#password").focus();
		} 
	});
}